package com.fencing_squad.homo.list;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class PostUser {
    private String nickname = "";
    private String date = "";
    private String content = "";
    private int likes = 0;
    private int comment = 0;
    private String avatar = "";
    private String image = "";
    private long timestamp = 0;
    private String postMessageId = "";

    public PostUser() {}

    public PostUser(JSONObject jsonObject) {
        JSONObject object = jsonObject.getJSONObject("post_message");
        comment = object.getIntValue("comment_num");
        content = object.getString("content");
        likes = object.getIntValue("like_num");
        timestamp = object.getLong("post_time");
        avatar = jsonObject.getString("avatar");
        nickname = jsonObject.getString("nick_name");
        postMessageId = object.getString("post_message_id");
        JSONArray imageList = jsonObject.getJSONArray("image_list");
        if (imageList != null && imageList.size() > 0) {
            image = imageList.getString(0);
        }
    }

    public PostUser(JSONObject jsonObject, int type) {
        JSONObject object = jsonObject.getJSONObject("result");
        comment = object.getIntValue("comment_num");
        content = object.getString("content");
        likes = object.getIntValue("like_num");
        timestamp = object.getLong("post_time");
        postMessageId = object.getString("post_message_id");
        avatar = jsonObject.getString("avatar");
        nickname = jsonObject.getString("nick_name");
        JSONArray imageList = jsonObject.getJSONArray("images");
        if (imageList != null && imageList.size() > 0) {
            image = imageList.getString(0);
        }
    }

    public String getPostMessageId() {
        return postMessageId;
    }

    public void setPostMessageId(String postMessageId) {
        this.postMessageId = postMessageId;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getDate() {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
        return format.format(timestamp);
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getLikes() {
        return String.valueOf(likes) + "点赞";
    }

    public void setLikes(int likes) {
        this.likes = likes;
    }

    public String getComment() {
        return String.valueOf(comment) + "评论";
    }

    public void setComment(int comment) {
        this.comment = comment;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public static List<PostUser> getFromJSONArray(JSONArray array) {
        if (array == null) {
            return null;
        }
        ArrayList<PostUser> posts = new ArrayList<>();
        for(int i=0; i<array.size(); i++) {
            posts.add(new PostUser(array.getJSONObject(i), 0));
        }
        return posts;
    }
}
